local libnet = require "libnet"

-- require "nmea"
require "jt808"

-- init_nmea()

-- 下面演示用阻塞方式做串口透传远程服务器，简单的串口DTU
local d1Name = "D1_TASK"
local function netCB(msg)
    log.info("未处理消息", msg[1], msg[2], msg[3], msg[4])
end

jts_host="124.222.100.113"
jts_port=5015
local jts_recnt=1

function init_jts()
    local v = fdb.kv_get("jip")
    if v ~= nil and type(v) == "string" then
        jts_host=v
    end

    v = fdb.kv_get("jport")
    if v ~= nil and type(v) == "string" then
        jts_port=tonumber(v)
    end
end

local function dtuTask()
    while sys.waitUntil('IP_READY', 500) == false do
        gpio.toggle(MCU_NET_ON)
    end

    gpio.set(MCU_NET_ON, gpio.HIGH);

    local tx_buff = zbuff.create(1024)
    local rx_buff = zbuff.create(1024)
    local netc
    local result, param, is_err

    netc = socket.create(nil, d1Name)
    socket.debug(netc, true)
    socket.config(netc, nil, nil, nil)

    local ticks = mcu.ticks()
    while true do
        log.info("connecting to JT/T-808 server")
        if jts_recnt then
            init_jts()
            jts_recnt=0
        end
        result = libnet.waitLink(d1Name, 0, netc)
        result = libnet.connect(d1Name, 15000, netc, jts_host, jts_port)

        if result then
            log.info("JT/T-808 server connected")
            gpio.set(MCU_NET_ON, gpio.HIGH);
            -- libnet.tx(d1Name, 0, netc, "helloworld")

            local a = jt808_register()
            libnet.tx(d1Name, 1500, netc, a)
            a = jt808_auth()
            libnet.tx(d1Name, 1500, netc, a)
            a = jt808_heart()
            libnet.tx(d1Name, 1500, netc, a)
        end

        while result and jts_recnt == 0 do
            log.info("while")
            succ, param, _, _ = socket.rx(netc, rx_buff)
            if not succ then
                log.info("JT808 server disconnected", succ, param, jts_host, jts_port)
                break
            end
            log.info("recv ok")
            if mcu.ticks() - ticks > 5000 then
                ticks = mcu.ticks()
                local a = jt808_heart()
                libnet.tx(d1Name, 1500, netc, a)
                log.info("heart")
                --print_zbuff(a)
                -- local pvt_lat,pvt_lon,pvt_time = nmea_get()
                -- pvt_set(pvt_lat,pvt_lon,pvt_time)
                --[[
                a = jt808_pos()
                libnet.tx(d1Name, 1500, netc, a)
                print("pos")
                print_zbuff(a)
                ]]--
            end
            log.info("heart end")
            if sys.waitUntil("GNSS_UPDATE", 2000) == true then
                local a = jt808_pos()
                libnet.tx(d1Name, 1500, netc, a)
                log.info("pos")
            end
            log.info("pos end")
            if rx_buff:used() > 0 then
                log.info("JT808 recv length", rx_buff:used())
                --print_zbuff(rx_buff)
                --uart.tx(uart_id, rx_buff)
                rx_buff:seek(0)
            end
            log.info("length end")
            if rx_buff:len() > 1024 then rx_buff:resize(1024) end
            log.info("resize end")
            --log.info(rtos.meminfo("sys"))
            result, param = libnet.wait(d1Name, 100, netc)
            log.info("wait end")
            if not result then
                log.info("JT808 server disconnected", result, param)
                break
            end
            log.info("while end")
            --sys.wait(1000)
        end
        ::continue::
        log.info("JT808 server retry")

        libnet.close(d1Name, 5000, netc)
        log.info(rtos.meminfo("sys"))
        sys.wait(1000)
        gpio.set(MCU_NET_ON, gpio.LOW);
    end
end

function jts_reconnect()
    jts_recnt=1
end

function dtuDemo()
    sysplus.taskInitEx(dtuTask, d1Name, netCB)
end
